import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class CardDemo {
    public static final String[] SUITS={"♠","♥", "♣", "♦"};

    //买一副牌
    private static List<Card> buyDeck(){
        List<Card> deck=new ArrayList<>(52);
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <=13; j++) {
                String suit=SUITS[i];//四种之一
                int rank=j;
                Card card=new Card();
                card.rank=rank;
                card.suit=suit;
                deck.add(card);
            }
        }
        return deck;
    }

    //交换牌的位置
    private static void swap(List<Card> deck,int i,int j){
        Card t=deck.get(i);
        deck.set(i,deck.get(j));
        deck.set(j,t);
    }

    //洗牌
    private static void shuffle(List<Card> deck){
        Random random=new Random(20190905);
        for (int i = deck.size()-1; i > 0; i--) {
            int r=random.nextInt(i);//每一张牌与别的牌进行交换
            swap(deck,i,r);
        }
    }

    public static void main(String[] args) {
        //1、买一副牌
        List<Card> deck=buyDeck();
        System.out.println("刚买回来的牌："+deck);
        //2、洗牌
        shuffle(deck);
        System.out.println("洗过的牌："+deck);

        //3、3个人，每人轮流抓5张牌
        List<List<Card>> hands=new ArrayList<>();//集合hands元素为各个成员的牌List<Card>
        hands.add(new ArrayList<>());
        hands.add(new ArrayList<>());
        hands.add(new ArrayList<>());

        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                hands.get(j).add(deck.remove(0));
            }
        }

        System.out.println("A手中的牌："+hands.get(0));
        System.out.println("B手中的牌："+hands.get(1));
        System.out.println("C手中的牌："+hands.get(2));
        System.out.println("剩余的牌："+deck);
    }
}
